/*
   ui_typw==20 CSV文本输入框
*/
(function ($) {
    $.fn.yqTextCSV = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTextCSV.methods[opts](this, param);
        }

        $this = this;
        var opt;
        if ($this.data("yqtype") == "yqTextCSV") {
            opt = $.extend({}, options($this), opts);
        }
        else {
            opt = $.extend({}, $.fn.yqTextCSV.defaults, opts);
            var $uniqueID = $this.attr("ID");
            if ($uniqueID === undefined) {
                $uniqueID = $.getUniqueID();
                $this.attr("ID", $uniqueID);
            }
        }

        if (opt.width === undefined && $this.width() > 0) {
            opt.width = $this.width();
        }
        else if (typeof opt.width != "undefined") {
            $this.width(opt.width);
        }

        //转换编辑尺寸
        opt.editModeW = $.whFormat(opt.editModeW);
        opt.editModeH = $.whFormat(opt.editModeH);

        //变量信息
        $this.data("textcsv_opt", opt);
        $this.data("yqtype", "yqTextCSV");

        init_text_before($this);
    };

    $.fn.yqTextCSV.defaults = {
        ui_type: 2,  //输入值类型 2:文本 4:日期 9:numberBox 10:numberSpinner,11timeSpinner,12:dateTimeSpinner
        ui_mask: {}, //掩码
        separator: ",", //分隔符
        value: "", //默认值
        isEdit: true,
        isAdd: true,
        isDel: true,
        isMove: true,
        editModeW: 500, //模态编辑器宽度 格式为 800（数字）
        editModeH: 300, //模态编辑器高度 格式为 600（数字）
        showEdit: true, //是否显示编辑按钮
        beforeBindingTitle: function (titles, callback) {
            callback(titles);
        }, //标题绑定前
        afterBindingTitle: function () { },   //标题绑定后

        beforeEditShow: function (edit_type, options, callback) {
            callback(true, edit_type, options);
        }, //弹出框显示前

        beforeAdd: function (rows, row, callback) {
            callback(row);
        },
        afterChange: function (value) {
        },
    };

    $.fn.yqTextCSV.methods = {
        options: function (jq) {
            return options(jq);
        },
        destroy: function (jq) {
            destroy(jq);
        },
        resize: function (jq, width) {
            resize(jq, width);
        },
        clear: function (jq, data) {
            var opt = options(jq);
            opt.afterChange.call(jq, null);
            setValue(jq, "");
        },
        setValue: function (jq, value) {
            setValue(jq, value);
        },
        getValue: function (jq) {
            return getValue(jq);
        },
        isEdit: function (jq, tf) {
            isEdit(jq, tf);
        },
        showEdit(jq, tf) {
            showEdit(jq, tf);
        },
    };

    function options($object) {
        return $object.data("textcsv_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text_before($object) {
        var opt = options($object);
        opt.beforeBindingTitle.call($object, opt.titles,
            function (titles) {
                opt.titles = titles;
                init_text($object);
            });
    }

    function init_text($object) {
        var opt = options($object);

        var text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'yqwin-combo-btn-icon fa fa-pencil',
                handler: function (e) {
                    var value = getValue($object);
                    opt.beforeEditShow.call($object, "yqTextCSV", {
                        ui_type: opt.ui_type,
                        ui_mask: opt.ui_mask,
                        separator: opt.separator,
                        value: value,
                        isEdit: opt.isEdit,
                        isAdd: opt.isAdd,
                        isDel: opt.isDel,
                        isMove: opt.isMove,
                        editModeW: opt.editModeW,
                        editModeH: opt.editModeH,
                        beforeAdd: opt.beforeAdd,
                    }, function (tf, edit_type, options) {
                        if (!tf) {
                            return;
                        }

                        var pp = $object.textbox("textbox");
                        prevent_e = $.getPreventDefault(pp[0]);
                        prevent_e.id = $object.attr("id");
                        prevent_e.options = opt;

                        $.pop_yqEditor[edit_type].call($object, prevent_e, options);
                    });
                }
            }, {
                iconCls: 'yqwin-combo-btn-icon fa fa-times',
                handler: function (e) {
                    opt.afterChange.call($object, null);
                    setValue($object, "");
                }
            }]
        };

        $object.textbox(text_opt);

        setValue($object, opt.value);
        isEdit($object, opt.isEdit);

        opt.afterBindingTitle.call($object);
    }

    function setValue($object, value) {
        var opt = options($object);
        if (Object.prototype.toString.call(value) === '[object Array]') {
            val = "";
            for (var i = 0; i < value.length; i++) {
                if (i > 0) {
                    val += opt.separator;
                }
                val += value[i];
            }
            value = val;
        }
        else if (value === undefined || value == null) {
            value = "";
        }
        $object.textbox("setText", value);
    }

    function getValue($object) {
        var value = $object.textbox("getText");
        return (value === "" ? null : value);
    }

    function showEdit($object, tf) {
        const opt = options($object);
        if(tf===undefined){
            tf = true;
        }

        opt.showEdit = tf;

        isEdit($object, opt.isEdit);
    }

    function isEdit($object, tf) {
        const opt = options($object);
        if(tf===undefined){
            tf = true;
        }
        const icon0 = $object.textbox("getIcon", 0);
        const icon1 = $object.textbox("getIcon", 1);
        const textbox = $object.textbox("textbox");

        if (opt.showEdit) {
            opt.isEdit = tf;
            icon0.show();
            if (opt.isEdit) {
                //icon0.removeClass('fa-pencil');
                //icon0.addClass('fa-pencil');
                textbox.width(opt.width - 52 - 3);
                icon1.show();
            } else {
                //icon0.removeClass('fa-pencil');
                //icon0.addClass('fa-pencil');
                textbox.width(opt.width - 26 - 3);
                icon1.hide();
            }
            $object.textbox("setEditable", false);
        } else {
            icon0.hide();
            icon1.hide();
            textbox.width(opt.width - 3);
            $object.textbox("setEditable", opt.isEdit);
        }
    }
})(jQuery);
